<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="区域">
              <j-select-depart placeholder="请选择区域" v-model="queryParam.areaId" @changeText="setChangeAreaName" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="分类">
              <a-select style="width: 220px" v-model="queryParam.type" placeholder="请选择分类">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="1">生活小区</a-select-option>
                <a-select-option value="2">工业园区</a-select-option>
                <a-select-option value="3">其他类</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="exportListToPDF" type="primary" icon="plus">打印清单</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <div v-if="false" class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="type" slot-scope="type">
          <span>{{ type | filterType }}</span>
        </template>
        <template slot="water" slot-scope="water">
          <span>{{ water | filterWater }}</span>
        </template>
        <template slot="shape" slot-scope="shape">
          <span>{{ shape | filterShape }}</span>
        </template>
        <template slot="material" slot-scope="material">
          <span>{{ material | filterMaterial }}</span>
        </template>
        <template slot="haveQuestion" slot-scope="haveQuestion">
          <span>{{ haveQuestion ? '有问题' : '无问题' }}</span>
        </template>
        <template slot="img" slot-scope="img">
          <span v-if="!img" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="img" alt="" @click="showImg(img)" style="max-width:40px; max-height: 40px;" />
        </template>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- pdf 导出报告 -->
    <div style="position:absolute; top: 0; left: 0">
      <wellCheckReport :area-name="selectAreaName" :report-list="reportList" ref="wellCheckReport" />
    </div>
  </a-card>
</template>

<script>
//  :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
import wellCheckReport from '@/views/components/Town/well-check-pdf-report.vue'
import { getCheckWellList, deleteCheckWell } from '@/api/wellCheck.js'
import { filterObj } from '@/utils/util'
import { checkWellAreaType, checkWellWaterType, checkWellShape, checkWellMaterial } from '@/config/enumeration.js'
export default {
  name: 'WellCheckLsit',
  filters: {
    filterType(type) {
      const typeItem = checkWellAreaType.find(item => item.value === type)
      if (typeItem) {
        return typeItem.label
      } else {
        return type
      }
    },
    filterWater(type) {
      const typeItem = checkWellWaterType.find(item => item.value === +type)
      if (typeItem) {
        return typeItem.label
      } else {
        return type
      }
    },
    filterShape(type) {
      const typeItem = checkWellShape.find(item => item.value === +type)
      if (typeItem) {
        return typeItem.label
      } else {
        return type
      }
    },
    filterMaterial(type) {
      const typeItem = checkWellMaterial.find(item => item.value === +type)
      if (typeItem) {
        return typeItem.label
      } else {
        return type
      }
    }
  },
  components: {
    wellCheckReport
  },
  data() {
    return {
      loading: false,
      queryParam: {
        areaId: '',
        type: ''
      },
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['5', '10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: (t, r, index) => {
            const { pageSize, current } = this.ipagination
            return (current - 1) * pageSize + parseInt(index) + 1
          }
        },
        {
          title: '区域',
          align: 'center',
          dataIndex: 'areaId_dictText'
        },
        {
          title: '分类',
          align: 'center',
          dataIndex: 'type',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '井编号',
          align: 'center',
          dataIndex: 'wellNo'
        },
        {
          title: '水质性状',
          align: 'center',
          dataIndex: 'water',
          scopedSlots: { customRender: 'water' }
        },
        {
          title: '形制',
          align: 'center',
          dataIndex: 'shape',
          scopedSlots: { customRender: 'shape' }
        },
        {
          title: '井盖材质',
          align: 'center',
          dataIndex: 'material',
          scopedSlots: { customRender: 'material' }
        },
        {
          title: '井深（m）',
          align: 'center',
          dataIndex: 'depth'
        },
        {
          title: '高程',
          align: 'center',
          dataIndex: 'height'
        },
        {
          title: '有无问题',
          align: 'center',
          dataIndex: 'haveQuestion',
          scopedSlots: { customRender: 'haveQuestion' }
        },
        {
          title: '井内照片',
          align: 'center',
          dataIndex: 'imageUrl',
          scopedSlots: { customRender: 'img' }
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'description'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys: [],
      selectionRows: [],
      selectAreaName: '',
      reportList: []
    }
  },
  created() {
    this.handleGetCheckWellList()
  },
  methods: {
    async handleGetCheckWellList(currentPage) {
      //加载数据 若传入参数1则加载第一页的内容
      if (currentPage === 1) {
        this.ipagination.current = 1
      }
      const params = this.getQueryParams() //查询条件
      this.loading = true
      const res = await getCheckWellList(params)
      if (res.success) {
        this.dataSource = res.result.records || res.result
        if (res.result.total) {
          this.ipagination.total = res.result.total
        } else {
          this.ipagination.total = 0
        }
      } else {
        this.$message.warning(res.message)
      }
      this.loading = false
    },
    async handleDelete(id) {
      const params = new URLSearchParams() // 创建对象
      params.append('id', id) // 封装对象
      await deleteCheckWell(params)
      this.handleGetCheckWellList(1)
    },
    setChangeAreaName(selectAreaName) {
      console.log(selectAreaName, 'selectAreaName')
      this.selectAreaName = selectAreaName
    },
    showImg(imgUrl) {
      this.$viewerApi({
        images: [imgUrl]
      })
    },
    getQueryParams() {
      //获取查询条件
      let param = Object.assign(this.queryParam)
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      param = filterObj(param)
      const paramKeys = Object.keys(param)
      const FormParams = new URLSearchParams() // 创建对象
      paramKeys.map(key => {
        FormParams.append(key, param[key]) // 封装对象
      })
      return FormParams
    },
    searchQuery() {
      this.handleGetCheckWellList(1)
    },
    searchReset() {
      this.queryParam = {
        areaId: '',
        type: ''
      }
      this.handleGetCheckWellList(1)
    },
    handleTableChange({ current, pageSize }) {
      this.ipagination.current = current
      this.ipagination.pageSize = pageSize
      this.handleGetCheckWellList()
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    async exportListToPDF() {
      if (!this.queryParam.areaId) {
        this.$message.warning('请先选择区域')
        return
      }
      let param = Object.assign(this.queryParam)
      param.pageNo = 1
      param.pageSize = 999999
      param = filterObj(param)
      const paramKeys = Object.keys(param)
      const FormParams = new URLSearchParams() // 创建对象
      paramKeys.map(key => {
        FormParams.append(key, param[key]) // 封装对象
      })
      const res = await getCheckWellList(FormParams)
      if (res.success) {
        const printData = res.result.records || res.result
        this.reportList = printData
        this.$nextTick(() => {
          this.$refs.wellCheckReport.exportPages()
        })
        console.log('printData', printData)
      } else {
        this.$message.warning(res.message)
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/less/common.less';
</style>
